package com.magnificent.atlantis.client.ui.planner.panels.message;

import com.magnificent.atlantis.client.AtlantisConstants;
import com.magnificent.atlantis.client.data.message.ClientMessageHelper;
import com.magnificent.atlantis.client.data.message.MessageType;
import com.magnificent.atlantis.client.data.message.messages.ClientMessage;
import com.magnificent.atlantis.client.ui.planner.PlannerData;
import com.magnificent.commons.utils.collections.CollectionsUtils;
import com.magnificent.helpers.CommonUIHelper;

import javax.swing.*;
import java.awt.*;

/**
 * User: Babanin
 * Date: 05.05.2009
 * Time: 23:05:49
 */
public class MessageHeaderPanel extends JPanel {
    private ClientMessage selectedClientTextMessage;

    private JLabel dateLabel;
    private JLabel usersLabel;
    private JLabel titleLabel;

    private PlannerData plannerData;

    public MessageHeaderPanel(PlannerData plannerData) {
        this.plannerData = plannerData;

        initHeaderControls();
        initHeaderView();
    }

    private void initHeaderControls() {
        setLayout(new BorderLayout());

        dateLabel = new JLabel();
        usersLabel = new JLabel();
        titleLabel = new JLabel();
    }

    private void initHeaderView() {
        setBorder(BorderFactory.createEmptyBorder(4, 3, 4, 3));
        add(CommonUIHelper.createVerticalBox(
                CommonUIHelper.packInPanel(usersLabel, BorderLayout.LINE_START),
                2,
                CommonUIHelper.createBorderLayoutPanel(
                        CollectionsUtils.array(
                                titleLabel,
                                dateLabel
                        ),
                        CollectionsUtils.array(
                                BorderLayout.LINE_START,
                                BorderLayout.LINE_END
                        )
                )
        ), BorderLayout.CENTER);
    }

    public void messageSelected(ClientMessage selectedClientTextMessage) {
        this.selectedClientTextMessage = selectedClientTextMessage;

        usersLabel.setText(generateUsersLabelText(selectedClientTextMessage));
        titleLabel.setText("<html><b>" + AtlantisConstants.MESSAGE_TITLE + "</b>" + selectedClientTextMessage.getTitle() + "</html>");
        dateLabel.setText("<html><b>" + AtlantisConstants.MESSAGE_DATE + "</b>" +
                AtlantisConstants.DATE_TIME_FORMAT.format(selectedClientTextMessage.getSentDate()) + "</html>");

        repaint();
    }

    private String generateUsersLabelText(ClientMessage selectedClientTextMessage) {
        StringBuilder builder = new StringBuilder();

        builder.append("<html><b>");

        if (selectedClientTextMessage.getMessageType() == MessageType.INBOX) {
            builder.append(AtlantisConstants.MESSAGE_SENDER + "</b> ").
                    append(ClientMessageHelper.senderToString(selectedClientTextMessage,
                            plannerData.getConfigurationContainer()));
        } else {
            builder.append(AtlantisConstants.MESSAGE_RECEIVER + "</b> ").
                    append(ClientMessageHelper.receiversToString(selectedClientTextMessage, plannerData));
        }

        builder.append("</html>");

        return builder.toString();
    }

    public void clear() {
        selectedClientTextMessage = null;

        usersLabel.setText("");
        titleLabel.setText("");
        dateLabel.setText("");

        repaint();
    }
}
